/**
 * 生成唯一ID（用于用户标识）
 */
export const generateUUID = (): string => {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    const r = Math.random() * 16 | 0;
    const v = c === 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
};

/**
 * 获取用户唯一标识（优先从localStorage取，没有则生成）
 */
export const getUserId = (): string => {
  let userId = localStorage.getItem('monitor_user_id');
  if (!userId) {
    userId = generateUUID();
    localStorage.setItem('monitor_user_id', userId);
  }
  return userId;
};

/**
 * 数据脱敏（过滤敏感字段）
 */
export const sanitizeData = (data: Record<string, any>): Record<string, any> => {
  const sensitiveKeys = ['password', 'pwd', 'token', 'idCard', 'phone'];
  const result = { ...data };
  sensitiveKeys.forEach(key => {
    if (result[key]) result[key] = '***';
  });
  return result;
};